#%%
import pandas as pd
import numpy as np
import plotly.express as px
import gc


df = pd.read_csv("logs_str_sum_01.csv")
df.drop("index", axis=1)
col = df.columns.tolist()
# print(col)
Dl = []
sortkey = []
sumkey = []
for key in col:
    if (
        "pre" in key
        or "Avg" in key
        or "std" in key
        or "sort" in key
        or "sum" in key
        or "MARKER" in key
        or "TYPES" in key
        or "Main" in key
    ):
        Dl.append(key)
        if "pre" in key:
            x = key
        elif "sum" in key:
            sumkey.append(key)
        elif "sort" in key:
            sortkey.append(key)

print(df.shape)
df = df[Dl]
#%%
def equares(df, keylist):
    data = []
    df = df[keylist]
    if keylist != []:
        key = keylist[0].split("_")[-1]
        for ind in range(df.shape[0]):
            t = df.iloc[ind]
            count = 0
            dic = {}
            for x in range(len(keylist) - 1):
                c1 = t.iloc[x]
                c2 = t.iloc[x + 1]
                if c1 == c2:
                    count += 1
            dic["index"] = ind
            if count == 4:
                dic[key + "E"] = 1
            else:
                dic[key + "E"] = 0
            data.append(dic)
        t = pd.DataFrame(data).drop("index", axis=1)
        return t


sortE = equares(df, sortkey)
df = pd.merge(df, sortE, left_index=True, right_index=True)
print("meraged")
sumE = equares(df, sumkey)
df = pd.merge(df, sumE, left_index=True, right_index=True)
print("meraged")

# %%
df["MARKER"] = df["MARKER=B"] + (-df["MARKER=S"])
df["MARKER"].fillna(0, inplace=True)
df[df["MARKER"] != 0].shape

#%%
rounds = df["TYPES"].tolist().count("ROUND")
high = df["TYPES"].tolist().count("HIGH")
low = df["TYPES"].tolist().count("LOW")
print(rounds, high, low)

#%%
def reformating(df, mode="1"):
    indL = []
    data2 = []
    for num in range(df.shape[0]):
        tdf = df.iloc[num]
        tdf_1 = df.iloc[num - 1]
        dic = {}
        if tdf["sortE"] == 1 or tdf["sumE"] == 1 and mode == "1":
            indL.append(num)
        elif tdf["sortE"] == 1 and tdf["sumE"] == 1 and mode == "2":
            indL.append(num)

            dic["index"] = num
            if tdf["All_sort"] > tdf_1["All_sort"]:
                dic["sort"] = 1
            elif tdf["All_sort"] < tdf_1["All_sort"]:
                dic["sort"] = -1

            if tdf["All_by%Boll_sum"] > tdf_1["All_by%Boll_sum"]:
                dic["sum"] = 1
            elif tdf["All_by%Boll_sum"] < tdf_1["All_by%Boll_sum"]:
                dic["sum"] = -1
            data2.append(dic)

        df = df.iloc[indL]
        df.reset_index(inplace=True)
        if data2 != []:
            tdf = pd.DataFrame(data2)
            df = pd.merge(df, tdf, on="index")
    print(df.shape)
    return df


# df = reformating(df, mode="1")

#%%
gbdf = df.groupby(["TYPES", x])
t = pd.DataFrame()
for k, v in gbdf:
    temp = v.describe(include="all").round(4)
    temp.loc["max_"] = temp.loc["mean"] - temp.loc["std"]
    temp.loc["min_"] = temp.loc["mean"] + temp.loc["std"]
    temp["gb"] = str(" ".join([str(key) for key in k]))
    temp.loc["temp"] = ""
    t = pd.concat([t, temp])
t.to_csv("des.csv")
df.corr().to_csv("corr.csv")

df[x] = df[x].apply(lambda x: "_" + str(x))
df["TAG"] = df["TYPES"] + df[x]

tagL = df["TAG"].unique().tolist()
tagL.sort()
# t = [
#     "yellowgreen",
#     "tomato",
#     "lightskyblue",
#     "lightpink",
#     "Aquamarine",
#     #    'aliceblue',
#     #    'mistyrose',
#     "pink",
#     "limegreen",
#     #    'skyblue',
#     "hotpink",
#     "cyan",
#     "gold",
#     "lightsalmon",
#     "green",
#     "orange",
#     "deeppink",
#     "Cyan",
#     "aqua",
#     "aquamarine",
#     "azure",
# ]
# t = t[: len(tagL) + 1]
# dic = dict(zip(tagL, t))
# print(dic)
# df["color"] = df["TAG"].apply(lambda x: dic[x])


#%%
for types in df["TYPES"].unique().tolist():
    _df = df[df["TYPES"] == types]
    features = _df.columns.tolist()
    L = [
        "TYPES",
        "TAG",
        "MARKER",
        "MARKER=S",
        "MARKER=B",
        # "sumE",
        # "sortE",
    ]
    L.extend(sumkey)
    L.extend(sortkey)
    for removekey in L:
        if "All" not in removekey:
            features.remove(removekey)

    fig = px.scatter_matrix(_df, dimensions=features, color="TAG")
    fig.update_traces(diagonal_visible=False)
    fig.write_html("px_{}.html".format(types))
# fig.show()

# %%
